if not __debug__ and re.search('x*', 'axx').span(0) == (0, 0):
raise AssertionError
if not __debug__ and re.search('x*', 'axx').span() == (0, 0):
raise AssertionError
if not __debug__ and re.search('x+', 'axx').span(0) == (1, 3):
raise AssertionError
if not __debug__ and re.search('x+', 'axx').span() == (1, 3):
raise AssertionError
if not __debug__ and re.search('x', 'aaa') == None:
raise AssertionError
except:
raise TestFailed, 're.search'
try:
if not __debug__ and re.match('a*', 'xxx').span(0) == (0, 0):
raise AssertionError
if not __debug__ and re.match('a*', 'xxx').span() == (0, 0):
raise AssertionError
if not __debug__ and re.match('x*', 'xxxa').span(0) == (0, 3):
raise AssertionError
if not __debug__ and re.match('x*', 'xxxa').span() == (0, 3):
raise AssertionError
if not __debug__ and re.match('a+', 'xxx') == None:
raise AssertionError
except:
raise TestFailed, 're.search'
if verbose:
print 'Running tests on re.sub'
try:
if not __debug__ and re.sub('(?i)b+', 'x', 'bbbb BBBB') == 'x x':
raise AssertionError
def bump_num(matchobj):
int_value = int(matchobj.group(0))
return str(int_value + 1)
if not __debug__ and re.sub('\\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y':
raise AssertionError
if not __debug__ and re.sub('\\d+', bump_num, '08.2 -2 23x99y', 3) == '9.3 -3 23x99y':
raise AssertionError
if not __debug__ and re.sub('.', (lambda m: '\\n'), 'x') == '\\n':
raise AssertionError
if not __debug__ and re.sub('.', '\\n', 'x') == '\n':
raise AssertionError
s = '\\1\\1'
if not __debug__ and re.sub('(.)', s, 'x') == 'xx':
raise AssertionError
if not __debug__ and re.sub('(.)', re.escape(s), 'x') == s:
raise AssertionError
if not __debug__ and re.sub('(.)', (lambda m: s), 'x') == s:
raise AssertionError
if not __debug__ and re.sub('(?P<a>x)', '\\g<a>\\g<a>', 'xx') == 'xxxx':
raise AssertionError
if not __debug__ and re.sub('(?P<a>x)', '\\g<a>\\g<1>', 'xx') == 'xxxx':
raise AssertionError
if not __debug__ and re.sub('(?P<unk>x)', '\\g<unk>\\g<unk>', 'xx') == 'xxxx':
raise AssertionError
if not __debug__ and re.sub('(?P<unk>x)', '\\g<1>\\g<1>', 'xx') == 'xxxx':
raise AssertionError
if not __debug__ and re.sub('a', '\\t\\n\\v\\r\\f\\a\\b\\B\\Z\\a\\A\\w\\W\\s\\S\\d\\D', 'a') == '\t\n\x0b\r\x0c\x07\x08\\B\\Z\x07\\A\\w\\W\\s\\S\\d\\D':
raise AssertionError
if not __debug__ and re.sub('a', '\t\n\x0b\r\x0c\x07', 'a') == '\t\n\x0b\r\x0c\x07':
raise AssertionError
if not __debug__ and re.sub('a', '\t\n\x0b\r\x0c\x07', 'a') == chr(9) + chr(10) + chr(11) + chr(13) + chr(12) + chr(7):
raise AssertionError
if not __debug__ and re.sub('^\\s*', 'X', 'test') == 'Xtest':
raise AssertionError
except AssertionError:
raise TestFailed, 're.sub'
try:
if not __debug__ and re.sub('a', 'b', 'aaaaa') == 'bbbbb':
raise AssertionError
if not __debug__ and re.sub('a', 'b', 'aaaaa', 1) == 'baaaa':
raise AssertionError
except AssertionError:
raise TestFailed, 'qualified re.sub'
if verbose:
print 'Running tests on symbolic references'
try:
re.sub('(?P<a>x)', '\\g<a', 'xx')
except re.error:
reason = None
raise TestFailed, 'symbolic reference'
try:
re.sub('(?P<a>x)', '\\g<', 'xx')
except re.error:
reason = None
raise TestFailed, 'symbolic reference'
try:
re.sub('(?P<a>x)', '\\g', 'xx')
except re.error:
reason = None
raise TestFailed, 'symbolic reference'
try:
re.sub('(?P<a>x)', '\\g<a a>', 'xx')
except re.error:
reason = None
raise TestFailed, 'symbolic reference'
try:
re.sub('(?P<a>x)', '\\g<1a1>', 'xx')
except re.error:
reason = None
raise TestFailed, 'symbolic reference'
try:
re.sub('(?P<a>x)', '\\g<ab>', 'xx')
except IndexError:
reason = None
raise TestFailed, 'symbolic reference'
try:
re.sub('(?P<a>x)|(?P<b>y)', '\\g<b>', 'xx')
except re.error:
reason = None
raise TestFailed, 'symbolic reference'
try:
re.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')
except re.error:
reason = None
raise TestFailed, 'symbolic reference'
if verbose:
print 'Running tests on re.subn'
try:
if not __debug__ and re.subn('(?i)b+', 'x', 'bbbb BBBB') == ('x x', 2):
raise AssertionError
if not __debug__ and re.subn('b+', 'x', 'bbbb BBBB') == ('x BBBB', 1):
raise AssertionError
if not __debug__ and re.subn('b+', 'x', 'xyz') == ('xyz', 0):
raise AssertionError
if not __debug__ and re.subn('b*', 'x', 'xyz') == ('xxxyxzx', 4):
raise AssertionError
if not __debug__ and re.subn('b*', 'x', 'xyz', 2) == ('xxxyz', 2):
raise AssertionError
except AssertionError:
raise TestFailed, 're.subn'
if verbose:
print 'Running tests on re.split'
try:
if not __debug__ and re.split(':', ':a:b::c') == [
'',
'a',
'b',
'',
'c']:
raise AssertionError
if not __debug__ and re.split(':*', ':a:b::c') == [
'',
'a',
'b',
'c']:
raise AssertionError
if not __debug__ and re.split('(:*)', ':a:b::c') == [
'',
':',
'a',
':',
'b',
'::',
'c']:
raise AssertionError
if not __debug__ and re.split('(?::*)', ':a:b::c') == [
'',
'a',
'b',
'c']:
raise AssertionError
if not __debug__ and re.split('(:)*', ':a:b::c') == [
'',
':',
'a',
':',
'b',
':',
'c']:
raise AssertionError
if not __debug__ and re.split('([b:]+)', ':a:b::c') == [
'',
':',
'a',
':b::',
'c']:
raise AssertionError
if not __debug__ and re.split('(b)|(:+)', ':a:b::c') == [
'',
None,
':',
'a',
None,
':',
'',
'b',
None,
'',
None,
'::',
'c']:
raise AssertionError
if not __debug__ and re.split('(?:b)|(?::+)', ':a:b::c') == [
'',
'a',
'',
'',
'c']:
raise AssertionError
except AssertionError:
raise TestFailed, 're.split'
try:
if not __debug__ and re.split(':', ':a:b::c', 2) == [
'',
'a',
'b::c']:
raise AssertionError
if not __debug__ and re.split(':', 'a:b:c:d', 2) == [
'a',
'b',
'c:d']:
raise AssertionError
if not __debug__ and re.split('(:)', ':a:b::c', 2) == [
'',
':',
'a',
':',
'b::c']:
raise AssertionError
if not __debug__ and re.split('(:*)', ':a:b::c', 2) == [
'',
':',
'a',
':',
'b::c']:
raise AssertionError
except AssertionError:
raise TestFailed, 'qualified re.split'
if verbose:
print 'Running tests on re.findall'
try:
if not __debug__ and re.findall(':+', 'abc') == []:
raise AssertionError
if not __debug__ and re.findall(':+', 'a:b::c:::d') == [
':',
'::',
':::']:
raise AssertionError
if not __debug__ and re.findall('(:+)', 'a:b::c:::d') == [
':',
'::',
':::']:
raise AssertionError
if not __debug__ and re.findall('(:)(:*)', 'a:b::c:::d') == [
(':', ''),
(':', ':'),
(':', '::')]:
raise AssertionError
except AssertionError:
raise TestFailed, 're.findall'
if verbose:
print 'Running tests on re.match'
try:
m = re.match('a', 'a')
if not __debug__ and m.groups() == ():
raise AssertionError
m = re.match('(a)', 'a')
if not __debug__ and m.groups() == ('a',):
raise AssertionError
pat = re.compile('((a)|(b))(c)?')
if not __debug__ and pat.match('a').groups() == ('a', 'a', None, None):
raise AssertionError
if not __debug__ and pat.match('b').groups() == ('b', None, 'b', None):
raise AssertionError
if not __debug__ and pat.match('ac').groups() == ('a', 'a', None, 'c'):
raise AssertionError
if not __debug__ and pat.match('bc').groups() == ('b', None, 'b', 'c'):
raise AssertionError
if not __debug__ and pat.match('bc').groups('') == ('b', '', 'b', 'c'):
raise AssertionError
except AssertionError:
raise TestFailed, 'match .groups() method'
try:
m = re.match('(a)', 'a')
if not __debug__ and m.group(0) == 'a':
raise AssertionError
if not __debug__ and m.group(0) == 'a':
raise AssertionError
if not __debug__ and m.group(1) == 'a':
raise AssertionError
if not __debug__ and m.group(1, 1) == ('a', 'a'):
raise AssertionError
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
if not __debug__ and pat.match('a').group(1, 2, 3) == ('a', None, None):
raise AssertionError
if not __debug__ and pat.match('b').group('a1', 'b2', 'c3') == (None, 'b', None):
raise AssertionError
if not __debug__ and pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c'):
raise AssertionError
except AssertionError:
raise TestFailed, 'match .group() method'
if verbose:
print 'Running tests on re.escape'
try:
p = ''
for i in range(0, 256):
p = p + chr(i)
if not __debug__ and re.match(re.escape(chr(i)), chr(i)) != None:
raise AssertionError
0
if not __debug__ and re.match(re.escape(chr(i)), chr(i)).span() == (0, 1):
raise AssertionError
range(0, 256)
pat = re.compile(re.escape(p))
if not __debug__ and pat.match(p) != None:
raise AssertionError
if not __debug__ and pat.match(p).span() == (0, 256):
raise AssertionError
except AssertionError:
raise TestFailed, 're.escape'
if verbose:
print 'Pickling a RegexObject instance'
import pickle
pat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
s = pickle.dumps(pat)
pat = pickle.loads(s)
try:
if not __debug__ and re.I == re.IGNORECASE:
raise AssertionError
if not __debug__ and re.L == re.LOCALE:
raise AssertionError
if not __debug__ and re.M == re.MULTILINE:
raise AssertionError
if not __debug__ and re.S == re.DOTALL:
raise AssertionError
if not __debug__ and re.X == re.VERBOSE:
raise AssertionError
except AssertionError:
raise TestFailed, 're module constants'
for flags in [
re.I,
re.M,
re.X,
re.S,
re.L]:
try:
r = re.compile('^pattern$', flags)
except:
0
[
re.I,
re.M,
re.X,
re.S,
re.L]
print 'Exception raised on flag', flags
*
for t in tests:
sys.stdout.flush()
if len(t) == 5:
(pattern, s, outcome, repl, expected) = t
elif len(t) == 3:
(pattern, s, outcome) = t
else:
raise ValueError, ('Test tuples should have 3 or 5 fields', t)
try:
obj = re.compile(pattern)
except re.error:
tests
tests
None if verbose else [
re.I,
re.M,
re.X,
re.S,
re.L]
if outcome == SYNTAX_ERROR:
pass
else:
print '=== Syntax error:', t
except KeyboardInterrupt:
raise KeyboardInterrupt
except:
tests
None if verbose else [
re.I,
re.M,
re.X,
re.S,
re.L]
print '*** Unexpected error ***', t
if verbose:
traceback.print_exc(file = sys.stdout)
try:
result = obj.search(s)
except re.error:
tests
msg = tests
None if verbose else [
re.I,
re.M,
re.X,
re.S,
re.L]
print '=== Unexpected exception', t, repr(msg)
except:
tests
if outcome == SYNTAX_ERROR:
pass
elif outcome == FAIL:
if result is None:
pass
else:
print '=== Succeeded incorrectly', t
elif outcome == SUCCEED:
if result is not None:
(start, end) = result.span(0)
vardict = {
'found': result.group(0),
'groups': result.group(),
'flags': result.re.flags }
for i in range(1, 100):
try:
gi = result.group(i)
if gi is None:
gi = 'None'
except IndexError:
0
0
range(1, 100)
gi = 'Error'
except:
0
vardict['g%d' % i] = gi
for i in result.re.groupindex.keys():
try:
gi = result.group(i)
if gi is None:
gi = 'None'
except IndexError:
0
0
result.re.groupindex.keys()
gi = 'Error'
except:
0
vardict[i] = gi
repl = eval(repl, vardict)
if repl != expected:
print '=== grouping error', t, repr(repl) + ' should be ' + repr(expected)
else:
print '=== Failed incorrectly', t
if pattern[:2] != '\\B' and pattern[-2:] != '\\B':